package com.atguigu.gulimall.common.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @author: create by sdl
 * @date:2021/5/27 11:57
 */
public class DateUtils {

    /**
     * 获取日期的年、月、日
     * @param dateObj
     */

    private static Date getDateFormat(Object dateObj) throws ParseException {

        Date date;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String name = dateObj.getClass().getName();
        if ("java.lang.String".equals(name)) {
            date = sdf.parse((String) dateObj);
        } else if ("java.lang.Long".equals(name)) {
            Long longDate = Long.valueOf((long)dateObj);
            date = new Date(longDate);
        }else {
            date = (Date)dateObj;
        }
        return date;
    }


    /**
     * 获取年
     * @param time
     * @return
     * @throws ParseException
     */
    public static String getYear(Object time) throws ParseException {
        Date date = getDateFormat(time);
        String year =String.format("%tY", date);
        return year;
    }

    /**
     * 获取月
     * @param time
     * @return
     * @throws ParseException
     */
    public static String getMonth(Object time) throws ParseException {
        Date date = getDateFormat(time);
        String month =String.format("%tm", date);
        return month;
    }

    /**
     * 获取日
     * @param time
     * @return
     * @throws ParseException
     */
    public static String getDay(Object time) throws ParseException {
        Date date = getDateFormat(time);
        String day =String.format("%td", date);
        return day;
    }

    /**
     * 计算两个日期之间相差 m周n天
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static String getBetweenWeekDay(String startDate, String endDate) throws ParseException {
        LocalDate localDate1 = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate localDate2 = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        long l = localDate2.toEpochDay() - localDate1.toEpochDay();
        long week = l / 7;
        long day = l % 7;
        String betweenWeekDay = week+"周";
        if (0 != day) {
            betweenWeekDay += day+"天";
        }
        return betweenWeekDay;
    }

    /**
     * 计算两个日期之间相差 n天
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static String getBetweenDay(String startDate, String endDate) throws ParseException {
        LocalDate localDate1 = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate localDate2 = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        long l = localDate2.toEpochDay() - localDate1.toEpochDay();
        return l+"天";
    }

    /**
     * 计算两个日期之间相差 m月n天
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static String getBetweenMonthDay(String startDate, String endDate) throws ParseException {
        LocalDate beforeDate = LocalDate.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate afterDate = LocalDate.parse(endDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        //月数
        long betweenMONTHS = ChronoUnit.MONTHS.between(beforeDate, afterDate);
        //逆推
//        LocalDate after = afterDate.minusMonths(betweenMONTHS);
//        long l = after.toEpochDay() - beforeDate.toEpochDay();
        //顺推
        LocalDate before = beforeDate.plusMonths(betweenMONTHS);
        long l = afterDate.toEpochDay() - before.toEpochDay();
        String monthDay = betweenMONTHS+"月";
        if (0 != l) {
            monthDay += l+"天";
        }
        return monthDay;
    }



    public static void main(String[] args) throws ParseException {
        String date1 = "2021-02-11";
        String date2 = "2021-06-01";

        String weekDay = getBetweenWeekDay(date1, date2);
        System.out.println("betWeenWeekDay===="+weekDay);
        String day = getBetweenDay(date1, date2);
        System.out.println("day==="+day);
        String betweenMonthDay = getBetweenMonthDay(date1, date2);
        System.out.println("monthDay==="+betweenMonthDay);
    }




}
